package nl.infodation.emr.pricingservice.ui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import nl.infodation.emr.pricingservice.controller.ControllerFactory;
import nl.infodation.emr.pricingservice.data.PublishedHistorySearchItem;
import nl.infodation.emr.pricingservice.entity.DomainRule;
import nl.infodation.emr.pricingservice.entity.PublishedHistory;
import nl.infodation.emr.pricingservice.ui.component.PagingComponent;
import nl.infodation.emr.pricingservice.ui.component.PagingModel;
import nl.infodation.emr.pricingservice.ui.page.SessionData;
import nl.infodation.emr.utility.Constants;

import com.vaadin.data.util.BeanItem;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings({"serial", "rawtypes"})
public class DomainRulePopup extends CustomComponent implements ItemClickListener{
	
	private VerticalLayout mainLayout;
	private DomainCheckList domainCheckList;

	private Label lblCurrentPage;
	private PagingComponent pagingComponent;
	private PagingModel model = new PagingModel(0);
	private VerticalLayout verticalLayout;
	
	private DomainRule domainRule;
	
	private ActiveDomainRuleListView parentView;

	public DomainRulePopup(ActiveDomainRuleListView activeDomainRuleListView,
			DomainRule domainRule) {
		this.parentView = activeDomainRuleListView;
		this.domainRule = domainRule;
		buildMainLayout();
		setCompositionRoot(mainLayout);
		initialModelPaging();
		domainCheckList.loadPage(1);
		//double click select Item
		domainCheckList.addListener(this);
	}
	
	private void initialModelPaging() {
		setTotalNumberOfPagesForReload();

		model.addPropertyChangeListener("currentPage",
				new PropertyChangeListener() {
					public void propertyChange(final PropertyChangeEvent evt) {
						updatePageNumber();
						domainCheckList.loadPage(model.getCurrentPage());
					}
				});
		pagingComponent.setModel(model);
		updatePageNumber();
	}

	private void updatePageNumber() {
		lblCurrentPage.setValue(SessionData.getI18N().getMessage(
				Constants.CURRENT_PAGE_VALUE)
				+ model.getCurrentPage());
	}

	private void setTotalNumberOfPagesForReload() {
		PublishedHistorySearchItem itemSearch = new PublishedHistorySearchItem();
		itemSearch.setDomainRuleID(domainRule.getDomainRuleId());
		final long totalItems = ControllerFactory.getPublishedHistoryController().getTotalNumberItemByCriteria(itemSearch);
		final int totalPages = (int) Math.ceil(totalItems
				/ (float) domainCheckList.getPageSize());
		model.setTotalNumberOfPages(totalPages);
	}
	
	@Override
	public void itemClick(ItemClickEvent event) {
		BeanItem bean = (BeanItem) event.getItem();
		PublishedHistory publishedDomain = (PublishedHistory) bean.getBean();
		parentView.rollBackRule(publishedDomain);	
		//close windows
		((Window) getWindow().getParent()).removeWindow(getWindow());
		
	}

	private void buildMainLayout() {
		mainLayout = new VerticalLayout();
		mainLayout.setImmediate(Boolean.FALSE);
		mainLayout.setSizeFull();
		mainLayout.setMargin(Boolean.FALSE);
		
		// top-level component properties
		setWidth("350px");
		setHeight("600px");
		
		domainCheckList = new DomainCheckList(domainRule);
		domainCheckList.setImmediate(Boolean.FALSE);
		domainCheckList.setWidth("100.0%");
		domainCheckList.setHeight("100.0%");
		domainCheckList.setStyleName("countrypopup");
		mainLayout.addComponent(domainCheckList);
		mainLayout.setExpandRatio(domainCheckList, 10);
		
		// verticalLayout
		verticalLayout = new VerticalLayout();
		verticalLayout.setImmediate(Boolean.FALSE);
		verticalLayout.setWidth("100.0%");
		verticalLayout.setHeight("70px");
		verticalLayout.setMargin(Boolean.FALSE);
		mainLayout.addComponent(verticalLayout);
		mainLayout.setExpandRatio(verticalLayout, 1);
		
		// pagingComponent_1
		pagingComponent = new PagingComponent();
		pagingComponent.setImmediate(Boolean.FALSE);
		verticalLayout.addComponent(pagingComponent);

		// lblCurrentPage
		lblCurrentPage = new Label();
		lblCurrentPage.setImmediate(Boolean.FALSE);
		lblCurrentPage.setValue("Current Page: ?");
		verticalLayout.addComponent(lblCurrentPage);
		
		
	}
}
